package com.csdn.leon.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.csdn.leon.db.SQLHelper;
import com.csdn.leon.model.BlogModel;
import com.csdn.leon.model.LabelModel;
import com.csdn.leon.model.Person;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBDao implements BaseDaoInface {
	private SQLHelper helper = null;
	private static final String[] CLOS = new String[] { "_ID", "username" };
	private static final String[] BLOGCLOS = new String[] { "_ID", "title" };
	public DBDao(Context context) {
		helper = new SQLHelper(context);
	}

	@Override
	public boolean addCache(LabelModel item) {
		// TODO Auto-generated method stub
		boolean flag = false;
		SQLiteDatabase database = null;
		long id = -1;
		try {
			database = helper.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put("name", item.getName());
			values.put("id", item.getId());
			values.put("orderId", item.getOrderId());
			values.put("selected", item.getSelected());
			values.put("blogtype", item.getBlogType());
			id = database.insert(SQLHelper.TABLE_CHANNEL, null, values);
			flag = (id != -1 ? true : false);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (database != null) {
				database.close();
			}
		}
		return flag;
	}

	@Override
	public boolean deleteCache(String whereClause, String[] whereArgs) {
		// TODO Auto-generated method stub
		boolean flag = false;
		SQLiteDatabase database = null;
		int count = 0;
		try {
			database = helper.getWritableDatabase();
			count = database.delete(SQLHelper.TABLE_CHANNEL, whereClause,
					whereArgs);
			flag = (count > 0 ? true : false);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (database != null) {
				database.close();
			}
		}
		return flag;
	}

	@Override
	public boolean updateCache(ContentValues values, String whereClause,
			String[] whereArgs) {
		// TODO Auto-generated method stub
		boolean flag = false;
		SQLiteDatabase database = null;
		int count = 0;
		try {
			database = helper.getWritableDatabase();
			count = database.update(SQLHelper.TABLE_CHANNEL, values,
					whereClause, whereArgs);
			flag = (count > 0 ? true : false);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (database != null) {
				database.close();
			}
		}
		return flag;
	}

	@Override
	public Map<String, String> viewCache(String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		SQLiteDatabase database = null;
		Cursor cursor = null;
		Map<String, String> map = new HashMap<String, String>();
		try {
			database = helper.getReadableDatabase();
			cursor = database.query(true, SQLHelper.TABLE_CHANNEL, null,
					selection, selectionArgs, null, null, null, null);
			int cols_len = cursor.getColumnCount();
			while (cursor.moveToNext()) {
				for (int i = 0; i < cols_len; i++) {
					String cols_name = cursor.getColumnName(i);
					String cols_values = cursor.getString(cursor
							.getColumnIndex(cols_name));
					if (cols_values == null) {
						cols_values = "";
					}
					map.put(cols_name, cols_values);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (database != null) {
				database.close();
			}
		}
		return map;
	}

	@Override
	public List<Map<String, String>> listCache(String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		SQLiteDatabase database = null;
		Cursor cursor = null;
		try {
			database = helper.getReadableDatabase();
			cursor = database.query(false, SQLHelper.TABLE_CHANNEL, null,
					selection, selectionArgs, null, null, null, null);
			int cols_len = cursor.getColumnCount();
			while (cursor.moveToNext()) {
				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < cols_len; i++) {

					String cols_name = cursor.getColumnName(i);
					String cols_values = cursor.getString(cursor
							.getColumnIndex(cols_name));
					if (cols_values == null) {
						cols_values = "";
					}
					map.put(cols_name, cols_values);
				}
				list.add(map);
			}

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (database != null) {
				database.close();
			}
		}
		return list;
	}

	public void clearFeedTable() {
		String sql = "DELETE FROM " + SQLHelper.TABLE_CHANNEL + ";";
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL(sql);
		revertSeq();
	}

	private void revertSeq() {
		String sql = "update sqlite_sequence set seq=0 where name='"
				+ SQLHelper.TABLE_CHANNEL + "'";
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL(sql);
	}

	@Override
	public boolean insertHost(Person person) {
		SQLiteDatabase database = null;
		boolean flag = false;
		database = helper.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put("username", person.getUsername());

		values.put("userface", person.getUserface());
		values.put("rank", person.getRank());
		values.put("statistics", person.getStatistics());

		database.insert("person", null, values);
		flag = true;

		return flag;
	}

	@Override
	public List<Person> getHostList() {
		SQLiteDatabase database = null;
		List<Person> list = new ArrayList<Person>();
		database = helper.getReadableDatabase();

		String sql = "select * from person";
		Cursor cursor = database.rawQuery(sql, null);
		System.out.println("sadf  cursor==" + cursor);
		cursor.moveToFirst();
		if (cursor.getCount() > 0) {
			do {
				Person person = new Person();
				// System.out.println("1.="+cursor.getString(1)+"   2.="+cursor.getString(2)+" 3.= "+cursor.getString(3)+
				// "5. "+cursor.getString(4)+"  "+cursor.getString(5)+" 6. "+cursor.getString(6)+"7."+cursor.getString(7)+
				// " 8. "+cursor.getString(8)+" 10."+cursor.getString(9)+" 11."+cursor.getString(10));

				person.setUsername(cursor.getString(1));

				person.setUserface(cursor.getString(2));
				person.setRank(cursor.getString(3));
				person.setStatistics(cursor.getString(4));
				list.add(person);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return list;
	}

	@Override
	public boolean queryHost(String name) {
		SQLiteDatabase database = null;
		database = helper.getReadableDatabase();

		Cursor cur = database.query(SQLHelper.USER_TABLE, CLOS, null, null,
				null, null, null);

		while (cur.moveToNext()) {
			if (cur.getString(cur.getColumnIndex("username")).equals(name)) {
				Log.e("判断是否关注", cur.getString(cur.getColumnIndex("username"))
						+ "");
				return true;
			}
		}

		// if(cur != null) {
		// for(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext()) {
		// int idColumn = cur.getColumnIndex(CLOS[0]);
		// int nameColumn = cur.getColumnIndex(CLOS[1]);
		// String id = cur.getString(idColumn);
		// String name1 = cur.getString(nameColumn);
		// if(null!=name1&&name1.equals("")){
		// return false;
		// }
		// }
		// cur.close();
		// }
		//

		return false;
	}

	@Override
	public boolean deleteHost(String whereArgs) {
		boolean flag = false;
		SQLiteDatabase database = null;
		int count = 0;
		try {
			database = helper.getWritableDatabase();
			count = database.delete(SQLHelper.USER_TABLE, SQLHelper.UERANAME
					+ "=?", new String[] { whereArgs });
			flag = (count > 0 ? true : false);
		} catch (Exception e) {
		} finally {
			if (database != null) {
				database.close();
			}
		}
		return flag;
	}

	@Override
	public boolean insertBlog(BlogModel blogModel) {
		SQLiteDatabase database = null;
		boolean flag = false;
		database = helper.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put("title", blogModel.getTitle());
		values.put("content", blogModel.getContent());
		values.put("date", blogModel.getDate());
		values.put("img", blogModel.getImgLink());
		values.put("link", blogModel.getLink());
		values.put("username", blogModel.getUsername());
		values.put("id", blogModel.getId());
		values.put("blogType", blogModel.getType());
		values.put("totalread", blogModel.getTotalread());
		values.put("totalcomment", blogModel.getTotalcomment());
		database.insert("blog", null, values);
		flag = true;
		System.out.println("宝成文件成功");

		return flag;

	}

	@Override
	public List<BlogModel> getBlogModelList() {
		SQLiteDatabase database = null;
		List<BlogModel> list = new ArrayList<BlogModel>();
		database = helper.getReadableDatabase();

		String sql = "select * from blog";
		Cursor cursor = database.rawQuery(sql, null);
		System.out.println("sadf  cursor==" + cursor);
		cursor.moveToFirst();
		if (cursor.getCount() > 0) {
			do {
				BlogModel item = new BlogModel();
				// System.out.println("1.="+cursor.getString(1)+"   2.="+cursor.getString(2)+" 3.= "+cursor.getString(3)+
				// "5. "+cursor.getString(4)+"  "+cursor.getString(5)+" 6. "+cursor.getString(6)+"7."+cursor.getString(7)+
				// " 8. "+cursor.getString(8)+" 10."+cursor.getString(9)+" 11."+cursor.getString(10));
				item.setTitle(cursor.getString(1));
				item.setContent(cursor.getString(2));
				item.setDate(cursor.getString(3));
				System.out.println("cursor.getCount====" + cursor.getCount());
				item.setImgLink(cursor.getString(4));
				System.out.println("55555555555555555555555555");
				item.setLink(cursor.getString(5));
				item.setUsername(cursor.getString(6));

				item.setId(cursor.getString(7));
				item.setTotalread(cursor.getString(9));
				item.setTotalcomment(cursor.getString(10));

				list.add(item);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return list;
	}

	@Override
	public boolean queryBlogModel(String title) {
		SQLiteDatabase database = null;
		database = helper.getReadableDatabase();

		Cursor cur = database.query(SQLHelper.BLOG_TABLE, BLOGCLOS, null, null,
				null, null, null);

		while (cur.moveToNext()) {
			String s = cur.getString(cur.getColumnIndex("title"));
			if (cur.getString(cur.getColumnIndex("title")).equals(title)) {
				Log.e("判断是否关注", cur.getString(cur.getColumnIndex("title")) + "");
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean deleteBlogModel(String whereArgs) {
		boolean flag = false;
		SQLiteDatabase database = null;
		int count = 0;
		try {
			database = helper.getWritableDatabase();
			count = database.delete(SQLHelper.BLOG_TABLE, SQLHelper.TITLE
					+ "=?", new String[] { whereArgs });
			flag = (count > 0 ? true : false);
		} catch (Exception e) {
		} finally {
			if (database != null) {
				database.close();
			}
		}
		return flag;
	}

}
